#include "stdafx.h"
#include "SendMessageError.h"

namespace RoomFW {

SendMessageError::SendMessageError(const char* sourceObjectName,
								   const PortIndex& portIndex,
								   MessageID messageId,
								   IPayloadSP payload,
								   Reason reason) :
	BaseError(ErrorHandling::EID_FND_SEND_MESSAGE, ErrorHandling::ESEVsoftwareWarning),
	portIndex_(portIndex),
	messageId_(messageId),
	payload_(payload),
	reason_(reason),
	portIndexText_(),
	messageIdText_()
{
	this->setSourceObjectName(sourceObjectName);
}

SendMessageError::SendMessageError(const PortIndex& portIndex,
								   MessageID messageId,
								   IPayloadSP payload,
								   Reason reason) :
	BaseError(ErrorHandling::EID_FND_SEND_MESSAGE, ErrorHandling::ESEVsoftwareWarning),
	portIndex_(portIndex),
	messageId_(messageId),
	payload_(payload),
	reason_(reason)
{
}

SendMessageError::~SendMessageError()
{
}

const char* SendMessageError::getName() const throw()
{
	return "SendMessageError";
}

std::string SendMessageError::toString() const
{
	std::ostringstream os;
	os << BaseError::toString();
	os << ", ";
	os << "payload=" << ((0 != payload_) ? payload_->toString() : "null");
	return os.str();
}

std::string SendMessageError::getDescription() const throw()
{
	std::ostringstream os;
	os << this->ReasonText(reason_);
	os << " [";
	if (portIndexText_.empty())
	{
		os << portIndex_.port();
	}
	else
	{
		os << portIndexText_;
	}
	os << "[" << portIndex_.replication() << "], ";
	if (messageIdText_.empty())
	{
		os << messageId_;
	}
	else
	{
		os << messageIdText_;
	}
	os << "]";
	return os.str();
}

const char* SendMessageError::ReasonText(const Reason reason)
{
	const char* retVal = "***Unknown***";
	switch (reason)
	{
	case SMER_NONE:
		retVal = "NONE";
		break;
	case SMER_INVALID_PORT:
		retVal = "INVALID_PORT";
		break;
	case SMER_PORT_NOT_BOUND:
		retVal = "PORT_NOT_BOUND";
		break;
	case SMER_RECEIVER_ERRORED:
		retVal = "RECEIVER_ERRORED";
		break;
	case SMER_BROADCAST_PAYLOAD:
		retVal = "BROADCAST_PAYLOAD";
		break;
	case SMER_BROADCAST_INVOKE:
		retVal = "BROADCAST_INVOKE";
		break;
	case SMER_CONTRACT_VIOLATION:
		retVal = "CONTRACT_VIOLATION";
		break;
	case SMER_REPLY_TO_REPLY:
		retVal = "REPLY_TO_REPLY";
		break;
	case SMER_INVOKE_TIMEOUT:
		retVal = "INVOKE_TIMEOUT";
		break;
	}
	return retVal;
}

PortIndex SendMessageError::getPortIndex()
{
	return portIndex_;
}

MessageID SendMessageError::getMessageId()
{
	return messageId_;
}

IPayloadSP SendMessageError::getPayload()
{
	return payload_;
}

IPayloadSP SendMessageError::releasePayload()
{
	return IPayload::ReleaseOwnership(payload_);
}

SendMessageError::Reason SendMessageError::getReason()
{
	return reason_;
}

void SendMessageError::setObjectName(const char* objectName)
{
	this->setSourceObjectName(objectName);
}

void SendMessageError::setPortIndexText(const char* portIndexText)
{
	portIndexText_ = portIndexText;
}

void SendMessageError::setMessageIdText(const char* messageIdText)
{
	messageIdText_ = messageIdText;
}

} // end namespace ErrorHandling

//BOOST_CLASS_EXPORT(RoomFW::SendMessageError);
BOOST_CLASS_EXPORT_KEY(RoomFW::SendMessageError);
